<?php
namespace app\admin\controller;
use app\common\controller\Common;
use app\admin\model\SupplierModel;
use app\common\model\Supplier as Suppliers;
use think\Exception;
use think\Request;

/**
* 供应商管理
*/
class Supplier extends Common
{
	/**
	* 添加供应商信息
	*/
	public function addsupplier()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(SupplierModel::serSupplier($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
		return $this->fetch();	
	}
	/**
	* 供应商列表
	*/
	public function supplierlist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			$where = [];
			if($info['sreach'])
			{
				$where['supplier_name|name'] = ['like','%'.$info['sreach'].'%'];
			}
			return (new Suppliers())->getSupplierList($where,10,$info['page']);
		}
		return $this->fetch();
	}
	/**
	* 删除
	*/
	public function delsupplier()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(SupplierModel::delSuppliers($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 编辑
	*/
	public function editsupplier()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				 return json(SupplierModel::updateSupplier($info));
			 }catch (\Exception $e){
				return json(back(config('status.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$this->assign('supplier',Suppliers::get($info['id']));
		return $this->fetch();	
	}
}
